﻿//Copyright (c) 2010-2011, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace yaTDL.Core.Converters
{
    /// <summary>
    /// An IValueConverter that converts a DateTime to a String.
    /// Aside from retuning DateTime.ToString() it is also capable of expressions like "Today", "Tomorrow" etc. and the name of the day if the day lies less than a week in the future.
    /// </summary>
    public class DateTimeStringConverter:IValueConverter
    {

        /// <summary>
        /// Converts a given DateTime value into a String.
        /// </summary>
        /// <param name="value">The DateTime to be converted.</param>
        /// <param name="targetType">The target type of the conversion. (It will be assumed that it is System.String)</param>
        /// <param name="parameter">The ConverterParameter. (Will be ignored in the current implementation)</param>
        /// <param name="culture">The CultureInfo for the conversion. This parameter will be ignored.</param>
        /// <returns>Returns the String generated from the given DateTime.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is DateTime)
            {
                DateTime d = (DateTime)value;

                TimeSpan difference = d.Date - DateTime.Now.Date;

                if (difference.TotalDays == 0)
                { return "Today"; }
                if (difference.TotalDays == 1)
                { return "Tomorrow"; }
                if (difference.TotalDays == -1)
                { return "Yesterday"; }
                if (difference.TotalDays <= 7 && difference.TotalDays > 1)
                { return d.DayOfWeek.ToString(); }
                if (d.Date == DateTime.Parse("01.01.0001").Date)
                { return ""; }
                else { return d.ToShortDateString(); }
            }
            else
            { return value; }
        }

        /// <summary>
        /// Converts a String back into a DateTime. This method not implemented yet.
        /// </summary>
        /// <param name="value">The String to be converted</param>
        /// <param name="targetType">The target type of the conversion.</param>
        /// <param name="parameter">The ConverterParameter. (Will be ignored in the current implementation)</param>
        /// <param name="culture">The CultureInfo for the conversion. This parameter will be ignored.</param>
        /// <returns>Throws an NotImplementedException.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
